package beans;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import lecture.LectureManagedBean;
import user.ScoreGroup;
import user.User;


/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="editScoreGroup")
@CustomScoped(value="#{window}")
public class EditScoreGroupsManagedBean {
    
    private User user;
    
    public List<ScoreGroup> getScoreGroups() {
        FacesContext c = FacesContext.getCurrentInstance();
        SessionManagedBean sb = c.getApplication().evaluateExpressionGet(c, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        List<ScoreGroup> sg = new ArrayList<ScoreGroup>();
        
        for (ScoreGroup s: sb.getUser().getScoreGroups()) {
            if (s.getLecture() == lecture)
                sg.add(s);
        }
        return sg;
    }
    
    public void addUser (ScoreGroup sg) {
        System.out.println("Adde User " + this.user.getUsername());
        sg.addUser(this.user);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public List<SelectItem> getUsers() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        for (User u: lecture.getStudents()) {
            list.add(new SelectItem(u, u.getUsername()));
        }
        return list;
    }
}
